package com.elitel.frame.base.controller;

import com.elitel.frame.base.component.quartz.SearchServiceTask;
import com.elitel.frame.base.service.SysDocumentService;
import com.elitel.frame.base.service.SysProjectService;
import com.elitel.frame.main.entity.SysDocument;
import com.elitel.frame.main.entity.SysProject;
import com.elitel.frame.main.entity.ext.SysDocumentExt;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * 项目非结构化文档管理Controller
 * created by guoyanfei on 2018/4/17
 */
@Controller
@RequestMapping("/system/systemDocument")
public class SystemDocumentController {
    private static final Logger logger = LoggerFactory.getLogger(SystemDocumentController.class);
    @Autowired
    private SysProjectService sysProjectService;
    @Autowired
    private SysDocumentService sysDocumentService;
    @Autowired
    private SearchServiceTask searchServiceTask;

    /**
     * 查询所有项目信息
     * created by 2018/04/17
     * @return SysProjectExt实体list集合
     * @author guoyanfei
     */
    @RequestMapping("/getProjectInfo")
    @ResponseBody
    public List<SysProject> getProjectInfo(){
        List<SysProject> sysProjectExts = new ArrayList<>();
        try{
            sysProjectExts = sysProjectService.selectPronotinDocument();
        }catch (Exception e){
            logger.error(e.getMessage(),e);
        }

        return sysProjectExts;
    }

    /**
     * 验证ftp服务器是否联通
     * created on 2018/04/18
     * @param request
     * @return true或false
     * @author guoyanfei
     */
    @RequestMapping("/ftpconnection")
    @ResponseBody
    public Boolean ftpconnection(HttpServletRequest request){
        Boolean isConn = false;
        try{
            String ftpHost = request.getParameter("ftpHost");
            int ftpPort = Integer.parseInt(request.getParameter("ftpPort"));
            String ftpUser = request.getParameter("ftpUser");
            String ftpPass = request.getParameter("ftpPass");
            isConn = sysDocumentService.ftpConnection(ftpHost,ftpPort,ftpUser,ftpPass);
        }catch (Exception e){
            e.printStackTrace();
        }
        return isConn;
    }

    /**
     * 新增非结构化文档信息
     * created on 2018/04/18
     * @param sysDocumentext 扩展实体
     * @return 新增条数
     */
    @RequestMapping("/insertSysDocument")
    @ResponseBody
    public Integer insertSysDocument(@RequestBody SysDocumentExt sysDocumentext){
        Integer result = 0;
        try{
            result = sysDocumentService.insertSysDocument(sysDocumentext);

            //启动任务调度Job任务
            searchServiceTask.startSearchDocumentScheduler(sysDocumentext.getGuid());
        }catch (Exception e){
            logger.error(e.getMessage(),e);
        }
        return result;
    }

    /**
     * 修改非结构化文档信息
     * created on 2018/04/18
     * @param sysDocumentext 扩展实体
     * @return 修改条数
     * @author guoyanfei
     */
    @RequestMapping("/updateSysDocument")
    @ResponseBody
    public Integer updateSysDocument(@RequestBody SysDocumentExt sysDocumentext){
        Integer result = 0;
        try {
            SysDocument sysDocument = new SysDocument();
            sysDocument.setGuid(sysDocumentext.getGuid());
            sysDocument.setServicename(sysDocumentext.getServicename());
            sysDocument.setFtphost(sysDocumentext.getFtphost());
            sysDocument.setFtpport(sysDocumentext.getFtpport());
            sysDocument.setFtpuser(sysDocumentext.getFtpuser());
            sysDocument.setFtppass(sysDocumentext.getFtppass());
            sysDocument.setDocpath(sysDocumentext.getDocpath());
            sysDocument.setIndextype(sysDocumentext.getIndextype());
            sysDocument.setCronexpression(sysDocumentext.getCronexpression());
            sysDocument.setRemark(sysDocumentext.getRemark());
            sysDocument.setDocsource(sysDocumentext.getDocsource());
            result = sysDocumentService.updateSysDocument(sysDocument);

            //修改任务调度Job任务
            searchServiceTask.updateSchedulerTime(sysDocumentext.getGuid(),sysDocumentext.getCronexpression());
        }catch (Exception e){
            logger.error(e.getMessage(),e);
        }
        return result;
    }

    /**
     * 根据主键id删除非结构化文档信息
     * created on 2018/04/19
     * @param request
     * @return 删除结果
     * @author guoyanfei
     */
    @RequestMapping("/deleteSysDocument")
    @ResponseBody
    public Integer deleteSysDocument(HttpServletRequest request){
        Integer result = 0;
        try{
            String procode = request.getParameter("procode");
            String guid = request.getParameter("guid");
            result = sysDocumentService.deleteSysDocument(guid,procode);

            //删除当前任务
            searchServiceTask.removeTask(procode);
        }catch (Exception e){
            logger.error(e.getMessage(),e);
        }
        return result;
    }

    /**
     * 查询服务ID是否存在
     * created on 2018/05/23
     * @param request
     * @return 查询条数
     * @author guoyanfei
     */
    @RequestMapping("/isExitServiceId")
    @ResponseBody
    public Integer isExitServiceId(HttpServletRequest request){
        Integer result = 0;
        try{
            String serviceId = request.getParameter("serviceId");
            result = sysDocumentService.isExitserviceId(serviceId);
        }catch (Exception e){
            logger.error(e.getMessage(),e);
        }

        return result;
    }

    /**
     * 通过文档ID查询文档检索服务配置信息
     * created on 2018/05/23
     * @param request
     * @return SysDocument 实体
     * @author guoyanfei
     */
    @RequestMapping("/selectInfoByguid")
    @ResponseBody
    public SysDocument selectInfoByguid(HttpServletRequest request){
        SysDocument sysDocument = new SysDocument();
        try{
            String guid = request.getParameter("guid");
            sysDocument = sysDocumentService.selectInfoByserviceId(guid);
        }catch (Exception e){
            logger.error(e.getMessage(),e);
        }

        return sysDocument;
    }

}
